<?php

namespace App\Http\Controllers\Admin;

use App\Http\Common\Lib\Common;
use App\Models\LotteryRecord;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LotteryRecordController extends BaseAdminController
{
    //
	private $model;
	public function __construct()
	{
		parent::__construct();
		$this->model = new LotteryRecord();
	}

	/**
	 * 列表
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View|\think\response\View
	 */
	public function index(){
		if (\request()->ajax()) {
			$page = \request()->input('page', 1);
			$pageSize = \request()->input('pageSize', 20);
			$search = \request()->input('search', '');
			$session = \request()->input('session', null);

			$pageData = $this->model->pages($page, $pageSize, $session, $search);
			return response()->json(['status' => $pageData['status'], 'msg' => $pageData['msg'], 'page' => $page, 'totalCount' => $pageData['data']['totalCount'], 'pages' => $pageData['data']['pages']]);
		} else {
			return view('admin.lotteryRecord.index');
		}
	}

	/**
	 * 用户的推荐记录
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View|\think\response\View
	 */
	public function userRecords(){
		if (\request()->ajax()) {
			$page = \request()->input('page', 1);
			$pageSize = \request()->input('pageSize', 20);
			$search = \request()->input('search', '');
			$start = \request()->input('start', '');
			$end = \request()->input('end', '');
			$userId = \request()->input('userId', 0);

			$pageData = $this->model->userRecords($userId,$page,$pageSize,$start,$end,$search);
			return response()->json(['status' => $pageData['status'], 'msg' => $pageData['msg'], 'page' => $page, 'totalCount' => $pageData['data']['totalCount'], 'pages' => $pageData['data']['pages']]);
		} else {
			return view('admin.lotteryRecord.records');
		}
	}

	public function statistic(){
		if(\request()->ajax()){
			$userId = \request()->input('id',0);
			$start = \request()->input('start','');
			$end = \request()->input('end','');
			$type = \request()->input('type','month');
			if(!$end){
				$end = date('Ymd');
			}
			$end = date('Ymd',strtotime($end)-3600*24);
			$info = $this->model->statistic($userId,$start,$end,$type);
			$data = $this->model->statisticData($userId,$start,$end,$type);
			return response()->json(Common::dataFormat(config('statusInfo.status.success'),config('statusInfo.status.success'),['info'=>$info,'matches'=>$data]));
		}else{
			return view('admin.lotteryRecord.statistic');
		}
	}

	/**
	 * 推荐添加
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
	 */
	public function add(){
		return view('admin.lotteryRecord.add');
	}

	public function addStore(){
		$input = [
			'user_id'=>\request()->input('user_id',0),
			'match_id'=>\request()->input('match_id',0),
			'result'=>\request()->input('result',[]),
		];
		$res = $this->model->add($input,true);
		return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
	}
	/**
	 * 推荐修改
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
	 */
	public function edit(){
		return view('admin.lotteryRecord.edit');
	}

	public function editStore(){
		$input = [
			'user_id'=>\request()->input('user_id',0),
			'match_id'=>\request()->input('match_id',0),
			'result'=>\request()->input('result',[]),
			'id'=>\request()->input('id',0),
		];
		//dd($input);
		$res = $this->model->edit($input,true);
		return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
	}

	/**
	 * 详情
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function getDetail(){
		$id = \request()->input('id',0);
		$res =$this->model->detail($id);
		return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
	}
}
